package schema

import (
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect/entsql"
	"entgo.io/ent/schema"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
	"entgo.io/ent/schema/index"
)

// ActReview 活动评价表实体
type ActReview struct {
	ent.Schema
}

// Fields 定义活动评价表的字段
func (ActReview) Fields() []ent.Field {
	return []ent.Field{
		field.Int64("id").
			Positive().
			Comment("评价ID").
			Unique().
			Immutable(),
		field.Int64("activity_id").
			Positive().
			Comment("活动ID"),
		field.Int64("user_id").
			Positive().
			Comment("评价用户ID"),
		field.Int("rating").
			Range(1, 5).
			Comment("评分(1-5星)"),
		field.Text("content").
			Optional().
			Comment("评价内容"),
		field.Bool("is_anonymous").
			Default(false).
			Comment("是否匿名评价"),
		field.Text("image_urls").
			Optional().
			Comment("评价图片URLs，多张图片以逗号分隔"),
		field.Time("created_at").
			Default(time.Now).
			Comment("创建时间"),
		field.Time("updated_at").
			Default(time.Now).
			UpdateDefault(time.Now).
			Comment("更新时间"),
		field.Text("admin_reply").
			Optional().
			Comment("管理员回复"),
		field.Time("replied_at").
			Optional().
			Nillable().
			Comment("回复时间"),
		field.Enum("status").
			Values("active", "deleted").
			Default("active").
			Comment("评价状态"),
	}
}

// Edges 定义与其他表的关系
func (ActReview) Edges() []ent.Edge {
	return []ent.Edge{
		// 与活动表关联
		edge.From("activity", Activity.Type).
			Ref("reviews").
			Field("activity_id").
			Unique().
			Required(),
		// 与用户表关联
		edge.From("user", User.Type).
			Ref("reviews").
			Field("user_id").
			Unique().
			Required(),
	}
}

// Indexes 添加索引
func (ActReview) Indexes() []ent.Index {
	return []ent.Index{
		// 活动ID索引，加速查询特定活动的所有评价
		index.Fields("activity_id"),
		// 用户ID索引，加速查询特定用户的所有评价
		index.Fields("user_id"),
		// 复合索引，确保一个用户对一个活动只能评价一次
		index.Fields("activity_id", "user_id").
			Unique(),
	}
}

// 表名映射
func (ActReview) Config() ent.Config {
	return ent.Config{
		Table: "act_reviews",
	}
}

// Annotations of the ConversationMember.
func (ActReview) Annotations() []schema.Annotation {
	return []schema.Annotation{
		entsql.Annotation{
			Table: "act_reviews",
		},
	}
}
